home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / posixfile.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  8KB  |  234 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Extended file operations available in POSIX.
  5.  
  6. f = posixfile.open(filename, [mode, [bufsize]])
  7.       will create a new posixfile object
  8.  
  9. f = posixfile.fileopen(fileobject)
  10.       will create a posixfile object from a builtin file object
  11.  
  12. f.file()
  13.       will return the original builtin file object
  14.  
  15. f.dup()
  16.       will return a new file object based on a new filedescriptor
  17.  
  18. f.dup2(fd)
  19.       will return a new file object based on the given filedescriptor
  20.  
  21. f.flags(mode)
  22.       will turn on the associated flag (merge)
  23.       mode can contain the following characters:
  24.  
  25.   (character representing a flag)
  26.       a       append only flag
  27.       c       close on exec flag
  28.       n       no delay flag
  29.       s       synchronization flag
  30.   (modifiers)
  31.       !       turn flags 'off' instead of default 'on'
  32.       =       copy flags 'as is' instead of default 'merge'
  33.       ?       return a string in which the characters represent the flags
  34.               that are set
  35.  
  36.       note: - the '!' and '=' modifiers are mutually exclusive.
  37.             - the '?' modifier will return the status of the flags after they
  38.               have been changed by other characters in the mode string
  39.  
  40. f.lock(mode [, len [, start [, whence]]])
  41.       will (un)lock a region
  42.       mode can contain the following characters:
  43.  
  44.   (character representing type of lock)
  45.       u       unlock
  46.       r       read lock
  47.       w       write lock
  48.   (modifiers)
  49.       |       wait until the lock can be granted
  50.       ?       return the first lock conflicting with the requested lock
  51.               or 'None' if there is no conflict. The lock returned is in the
  52.               format (mode, len, start, whence, pid) where mode is a
  53.               character representing the type of lock ('r' or 'w')
  54.  
  55.       note: - the '?' modifier prevents a region from being locked; it is
  56.               query only
  57. """
  58. import warnings
  59. warnings.warn('The posixfile module is deprecated; fcntl.lockf() provides better locking', DeprecationWarning, 2)
  60.  
  61. class _posixfile_:
  62.     '''File wrapper class that provides extra POSIX file routines.'''
  63.     states = [
  64.         'open',
  65.         'closed']
  66.     
  67.     def __repr__(self):
  68.         file = self._file_
  69.         return "<%s posixfile '%s', mode '%s' at %s>" % (self.states[file.closed], file.name, file.mode, hex(id(self))[2:])
  70.  
  71.     
  72.     def open(self, name, mode = 'r', bufsize = -1):
  73.         import __builtin__ as __builtin__
  74.         return self.fileopen(__builtin__.open(name, mode, bufsize))
  75.  
  76.     
  77.     def fileopen(self, file):
  78.         import types as types
  79.         if repr(type(file)) != "<type 'file'>":
  80.             raise TypeError, 'posixfile.fileopen() arg must be file object'
  81.         repr(type(file)) != "<type 'file'>"
  82.         self._file_ = file
  83.         for maybemethod in dir(file):
  84.             if not maybemethod.startswith('_'):
  85.                 attr = getattr(file, maybemethod)
  86.                 if isinstance(attr, types.BuiltinMethodType):
  87.                     setattr(self, maybemethod, attr)
  88.                 
  89.             isinstance(attr, types.BuiltinMethodType)
  90.         
  91.         return self
  92.  
  93.     
  94.     def file(self):
  95.         return self._file_
  96.  
  97.     
  98.     def dup(self):
  99.         import posix as posix
  100.         if not hasattr(posix, 'fdopen'):
  101.             raise AttributeError, 'dup() method unavailable'
  102.         hasattr(posix, 'fdopen')
  103.         return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
  104.  
  105.     
  106.     def dup2(self, fd):
  107.         import posix
  108.         if not hasattr(posix, 'fdopen'):
  109.             raise AttributeError, 'dup() method unavailable'
  110.         hasattr(posix, 'fdopen')
  111.         posix.dup2(self._file_.fileno(), fd)
  112.         return posix.fdopen(fd, self._file_.mode)
  113.  
  114.     
  115.     def flags(self, *which):
  116.         import fcntl as fcntl
  117.         import os as os
  118.         if which:
  119.             if len(which) > 1:
  120.                 raise TypeError, 'Too many arguments'
  121.             len(which) > 1
  122.             which = which[0]
  123.         else:
  124.             which = '?'
  125.         l_flags = 0
  126.         if 'n' in which:
  127.             l_flags = l_flags | os.O_NDELAY
  128.         
  129.         if 'a' in which:
  130.             l_flags = l_flags | os.O_APPEND
  131.         
  132.         if 's' in which:
  133.             l_flags = l_flags | os.O_SYNC
  134.         
  135.         file = self._file_
  136.         if '=' not in which:
  137.             cur_fl = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0)
  138.             if '!' in which:
  139.                 l_flags = cur_fl & ~l_flags
  140.             else:
  141.                 l_flags = cur_fl | l_flags
  142.         
  143.         l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFL, l_flags)
  144.         if 'c' in which:
  145.             arg = '!' not in which
  146.             l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFD, arg)
  147.         
  148.         if '?' in which:
  149.             which = ''
  150.             l_flags = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0)
  151.             if os.O_APPEND & l_flags:
  152.                 which = which + 'a'
  153.             
  154.             if fcntl.fcntl(file.fileno(), fcntl.F_GETFD, 0) & 1:
  155.                 which = which + 'c'
  156.             
  157.             if os.O_NDELAY & l_flags:
  158.                 which = which + 'n'
  159.             
  160.             if os.O_SYNC & l_flags:
  161.                 which = which + 's'
  162.             
  163.             return which
  164.  
  165.     
  166.     def lock(self, how, *args):
  167.         import struct as struct
  168.         import fcntl
  169.         if 'w' in how:
  170.             l_type = fcntl.F_WRLCK
  171.         elif 'r' in how:
  172.             l_type = fcntl.F_RDLCK
  173.         elif 'u' in how:
  174.             l_type = fcntl.F_UNLCK
  175.         else:
  176.             raise TypeError, 'no type of lock specified'
  177.         if 'w' in how in how:
  178.             cmd = fcntl.F_SETLKW
  179.         elif '?' in how:
  180.             cmd = fcntl.F_GETLK
  181.         else:
  182.             cmd = fcntl.F_SETLK
  183.         l_whence = 0
  184.         l_start = 0
  185.         l_len = 0
  186.         if len(args) == 1:
  187.             l_len = args[0]
  188.         elif len(args) == 2:
  189.             (l_len, l_start) = args
  190.         elif len(args) == 3:
  191.             (l_len, l_start, l_whence) = args
  192.         elif len(args) > 3:
  193.             raise TypeError, 'too many arguments'
  194.         
  195.         import sys as sys
  196.         import os
  197.         if sys.platform in ('netbsd1', 'openbsd2', 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8', 'bsdos2', 'bsdos3', 'bsdos4'):
  198.             flock = struct.pack('lxxxxlxxxxlhh', l_start, l_len, os.getpid(), l_type, l_whence)
  199.         elif sys.platform in ('aix3', 'aix4'):
  200.             flock = struct.pack('hhlllii', l_type, l_whence, l_start, l_len, 0, 0, 0)
  201.         else:
  202.             flock = struct.pack('hhllhh', l_type, l_whence, l_start, l_len, 0, 0)
  203.         flock = fcntl.fcntl(self._file_.fileno(), cmd, flock)
  204.         if '?' in how:
  205.             if sys.platform in ('netbsd1', 'openbsd2', 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 'bsdos2', 'bsdos3', 'bsdos4'):
  206.                 (l_start, l_len, l_pid, l_type, l_whence) = struct.unpack('lxxxxlxxxxlhh', flock)
  207.             elif sys.platform in ('aix3', 'aix4'):
  208.                 (l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs) = struct.unpack('hhlllii', flock)
  209.             elif sys.platform == 'linux2':
  210.                 (l_type, l_whence, l_start, l_len, l_pid, l_sysid) = struct.unpack('hhllhh', flock)
  211.             else:
  212.                 (l_type, l_whence, l_start, l_len, l_sysid, l_pid) = struct.unpack('hhllhh', flock)
  213.             if l_type != fcntl.F_UNLCK:
  214.                 if l_type == fcntl.F_RDLCK:
  215.                     return ('r', l_len, l_start, l_whence, l_pid)
  216.                 return ('w', l_len, l_start, l_whence, l_pid)
  217.             l_type != fcntl.F_UNLCK
  218.         
  219.  
  220.  
  221.  
  222. def open(name, mode = 'r', bufsize = -1):
  223.     '''Public routine to open a file as a posixfile object.'''
  224.     return _posixfile_().open(name, mode, bufsize)
  225.  
  226.  
  227. def fileopen(file):
  228.     '''Public routine to get a posixfile object from a Python file object.'''
  229.     return _posixfile_().fileopen(file)
  230.  
  231. SEEK_SET = 0
  232. SEEK_CUR = 1
  233. SEEK_END = 2
  234.